package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @ClassName: SysRoleController
 * @Package: com.atguigu.spzx.manager.controller
 * @Description 角色管理的控制层
 * @Author 孤妤
 * @Date: 2023/12/4 10:44
 */
@RestController
@RequestMapping( "/admin/system/sysRole")
@Tag(name = "角色管理")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @GetMapping("/{pageNum}/{pageSize}")
    @Operation(summary = "模糊查询与分页")
    public Result<PageInfo<SysRole>> findByPage(SysRoleDto sysRoleDto ,
                                                @PathVariable(value = "pageNum") Integer pageNum ,
                                                @PathVariable(value = "pageSize") Integer pageSize) {
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(sysRoleDto , pageNum , pageSize) ;
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }
    @PostMapping("/save")
    @Operation(summary = "添加角色")
    public Result addSysRole(@RequestBody SysRole sysRole){
        sysRoleService.addSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg("添加成功");
    }
    @DeleteMapping("/deleteById/{id}")
    @Operation(summary = "删除角色")
    public Result removeSysRole(@PathVariable Long id){
        sysRoleService.removeById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS).setMsg("删除成功");
    }
    @GetMapping("/findById/{id}")
    @Operation(summary = "根据id查询角色信息")
    public Result findById(@PathVariable Long id){
        SysRole sysRole =  sysRoleService.findById(id);
        System.out.println("sysRole = " + sysRole);
        return Result.build(sysRole,ResultCodeEnum.SUCCESS);
    }
    @PutMapping("/updateSysRole")
    @Operation(summary = "修改角色信息")
    public Result updateSysRole(@RequestBody SysRole sysRole) {
        sysRoleService.changeSysRole(sysRole) ;
        return Result.build(null , ResultCodeEnum.SUCCESS).setMsg("修改成功") ;
    }
    @GetMapping("/getSysRoleList")
    @Operation(summary = "查询所有角色")
    public Result<Map<String, Object>> getSysRoleList(){
        Map<String,Object> sysRoleMap = sysRoleService.getSysRoleList();
        return Result.build(sysRoleMap,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/findRoleAssign/{id}")
    @Operation(summary = "根据用户id查询角色和信息")
    public Result<Map<String, Object>> findRoleAssign(@PathVariable Long id){
        Map<String, Object> resultMap = sysRoleService.findAllRoles(id);
        return Result.build(resultMap , ResultCodeEnum.SUCCESS);
    }
}
